﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HU8.Helpers.Files;

namespace HU8.Web.FileExplorer.FileExplorer
{
    public partial class TextEditor : FileExplorerPage
    {
        private string rootPath = "~/";
        protected void Page_Load(object sender, EventArgs e)
        {
            string path = Request["Path"];
            string basePath = Request["BasePath"];

            if (path != null)
            {
                FileExplorerConfig config = new FileExplorerConfig(Server);
                if (!string.IsNullOrEmpty(basePath))
                {
                    litScript.Text += string.Format("var ori_path = \"{0}\";", path);
                    if (!path.StartsWith("http"))
                    {
                        path = Path.Combine(basePath, path); //传入一个 ~/Administrator/ 和 ../relative/path组合一下
                    }
                }
                else
                {
                    path = path.Replace("~/", config.BasePath); //在素材管理器里直接管理
                }
                string physicalPath = Server.MapPath(path);
                litScript.Text += string.Format("var syntax = '{0}';\n", GetFileSyntax(physicalPath));
                litScript.Text += string.Format("var base_url = \"{0}\";", VirtualPathUtility.ToAbsolute(rootPath));
                litScript.Text += string.Format("var file_path = \"{0}\";", path);

                if (File.Exists(physicalPath))
                {
                    tbEditor.Text = FileHelper.ReadTextFile(physicalPath);
                }
                else
                {
                    //不存在
                    pnlNoFile.Visible = true;
                    pnlEditor.Visible = false;

                }
            }
            else
            {
                pnlNoFile.Visible = true;
                pnlEditor.Visible = false;
            }
        }
        private string GetFileSyntax(string path)
        {
            FileInfo fi = new FileInfo(path);
            if (".xml,.config".Contains(fi.Extension))
            {
                return "xml";
            }
            else if (".html,.htm,.htmlx,.asp,.aspx".Contains(fi.Extension))
            {
                return "html";
            }
            else if (".js".Contains(fi.Extension))
            {
                return "js";
            }
            else if (".css".Contains(fi.Extension))
            {
                return "css";
            }
            else if (".php".Contains(fi.Extension))
            {
                return "php";
            }
            else if (".vb,.cs".Contains(fi.Extension))
            {
                return "vb";
            }
            else if (".sql".Contains(fi.Extension))
            {
                return "sql";
            }
            return "brainfuck";
        }
    }
}